package exe;

import java.io.File;
import java.io.FilenameFilter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-06-14
 * Time: 15:22
 */
public class Test {
    public static long getDirectorySize(File file) {
        long size = 0;
        if(file.isFile()) {
            size = file.length();
        } else {
            File[] all = file.listFiles();
            for (File f :
                    all) {
                size += getDirectorySize(f);
            }
        }
        return size;
    }
    public static void deleteDirectory(File file) {
        if(file.isDirectory()) {
            File[] all = file.listFiles();
            for (File f :
                    all) {
                deleteDirectory(f);
            }
        }
        file.delete();
    }
    public static void printFileName(File file) {
        if (file.isFile()) {
            System.out.println(file.getName());
        } else if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f :
                    files) {
                printFileName(f);
            }
        }
    }
    public static void main(String[] args) {
//        printFileName(new File("D:\\JAVA\\java\\j230614"));
        long l = getDirectorySize(new File("D:\\JAVA\\java\\j230614"));
        System.out.println(l);
    }
    public static void main2(String[] args) {
        File file = new File("C:\\Users\\86185\\Pictures\\Saved Pictures");
        //method 1:
//        String[] list = file.list();
//        for (String s :
//                list) {
//            if (s.endsWith(".jpg")) {
//                System.out.println(s);
//            }
//        }
        //method 2:
        String[] list = file.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jpg");
            }
        });
        for (String s :
                list) {
            System.out.println(s);
        }
    }
    public static void main1(String[] args) {
        File file = new File("hello.txt");
        System.out.println(file.getAbsolutePath());
//        file.getAbsoluteFile().getParent();
        File file1 = new File(file.getAbsoluteFile().getParent(), "abc.txt");
        System.out.println(file1.getAbsoluteFile());
    }
}
